package com.example.demo.controller;

import com.example.demo.custom.CustomResponse;
import com.example.demo.dto.PageInfoDto;
import com.example.demo.exception.UserException;
import com.example.demo.model.User;
import com.example.demo.model.UserLog;
import com.example.demo.service.Impl.UserLogServiceImpl;
import com.example.demo.service.IuserLogService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.AllArgsConstructor;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/userLog")
@AllArgsConstructor
public class UserLogController {

    private final IuserLogService iuserLogService;

    @PostMapping("/create")
    public CustomResponse createUser(@RequestBody UserLog userlog) {
        String message;
        try {
            iuserLogService.insertUserLog(userlog);
            message = "创建成功";
        } catch (UserException e) {
            message = e.getMessage();
        }catch (Exception e){
            message = e.getMessage();
        }
        return new CustomResponse(message);
    }

    @GetMapping("/getAllUserLog")
    public PageInfo getAllUserLog(@RequestParam Long userId, @RequestParam PageInfoDto pageInfoDto) {
        PageHelper.startPage(pageInfoDto.getPage(), pageInfoDto.getSize());
        List<UserLog> userLogs = iuserLogService.getAllUserLog();
        PageInfo<UserLog> pageInfo = new PageInfo<>(userLogs);
        return pageInfo;
    }

    @DeleteMapping("/delete/{id}")
    public CustomResponse deleteUserLog(@PathVariable Long userId) {
        iuserLogService.deleteUserLogByUserId(userId);
        return new CustomResponse("删除成功");
    }
}
